package cn.com.chengmandian.bike.controller.rent;

import cn.com.chengmandian.bike.service.IEbRentOrderService;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.service.bike.form.RentEndOrderForm;
import cn.com.chengmandian.service.bike.form.RentRefundForm;
import cn.com.chengmandian.service.bike.form.RentReturnBikeForm;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Slf4j
@Api(tags = "租赁相关接口 - 微服务")
@RestController
@RequestMapping("/inner/rent")
public class InnerRentController {

    @Autowired
    private IEbRentOrderService rentOrderService;

    @ApiOperation("租赁-结束订单")
    @PostMapping("/end/order")
    public AjaxResult endOrder(@RequestBody RentEndOrderForm form){
        Assert.notNull(form.getOrderId(), "缺少订单id");
        Assert.notNull(form.getAgentUser(), "缺少操作用户");
        rentOrderService.endOrder(form,  true);
        return AjaxResult.success();
    }

    @ApiOperation("租赁-强制还车")
    @PostMapping("/return/order")
    public AjaxResult returnOrder(@RequestBody RentReturnBikeForm form){
        Assert.isTrue(StrUtil.isNotBlank(form.getBikeNo()), "缺少车辆编号");
        rentOrderService.returnOrder(form, true);
        return AjaxResult.success();
    }

    @ApiOperation("租赁-金额退款")
    @PostMapping("/refund")
    public AjaxResult rentRefund(@RequestBody RentRefundForm form){
        Assert.notNull(form.getPayId(), "缺少支付id");
        Assert.notNull(form.getOrderId(), "缺少订单id");
        Assert.isTrue(form.getRefundMoney() != null && form.getRefundMoney().compareTo(BigDecimal.ZERO) > 0, "缺少金额且金额必须大于0");
        Assert.notNull(form.getLoginUser(), "缺少操作人");
        rentOrderService.rentRefund(form);
        return AjaxResult.success();
    }
}
